// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

#ifndef CANGJIE_MIGRATOR_UTIL_ENUM
#define CANGJIE_MIGRATOR_UTIL_ENUM

#include <string>

namespace CangjieMigrator::Util {

template <auto value>
constexpr auto EnumName()
{
    std::string_view name;
#if __GNUC__ || __clang__
    name = __PRETTY_FUNCTION__;
    std::size_t start = name.find('=') + 2;
    std::size_t end = name.size() - 1;
    name = std::string_view{name.data() + start, end - start};
    start = name.rfind("::");
#elif _MSC_VER
    name = __FUNCSIG__;
    std::size_t start = name.find('<') + 1;
    std::size_t end = name.rfind(">(");
    name = std::string_view{name.data() + start, end - start};
    start = name.rfind("::");
#endif
    return std::string(
        start == std::string_view::npos ? name : std::string_view{name.data() + start + 2, name.size() - start - 2});
}

template <typename T, std::size_t N = 0>
constexpr auto EnumMax()
{
    constexpr auto value = static_cast<T>(N);
    if constexpr (EnumName<value>().find(")") == std::string_view::npos) {
        return EnumName<T, N + 1>();
    } else {
        return N;
    }
}

} // namespace CangjieMigrator::Util

#endif // CANGJIE_MIGRATOR_UTIL_ENUM
